package com.xiaolin.io;

import java.io.*;
import java.util.Scanner;

/**
 * @ClassName TestPrintWriter
 * @Description TODO
 * @Detail 测试支付打印流
 * @Author MyPC
 * @Date 2020/9/26
 * @Version 1.0
 */
public class TestPrintWriter {
    public static void main(String[] args) throws FileNotFoundException {
        printToFile("D:\\1file\\out.txt");
    }

    /**
     * 将控制台打印的信息输出到文件中
     * @param dest
     */
    public static void printToFile(String dest){
        FileOutputStream fileOutputStream = null;
        PrintWriter printWriter = null;
        try {
            fileOutputStream = new FileOutputStream(dest);
            printWriter=new PrintWriter(fileOutputStream);
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要打印的信息，下一行输入quit按回车则退出程序");
            String in="";
            while (true){
                in = scanner.nextLine();
                if("quit".equals(in)){
                    break;
                }
                printWriter.println(in);
            }
            System.out.println("输入结束。。");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(printWriter !=null){
                printWriter.close();
            }
            if(fileOutputStream !=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
